import { Component } from '@angular/core';
import {Config, IonicPage, NavController, NavParams} from 'ionic-angular';
import {FormBuilder, FormGroup, Validators} from "@angular/forms";
import {ControlsCheck} from "../../common/controlsCheck";
import {UserProvider} from "../../providers/user/user";
import {SubPage} from "../../common/subPage";

@IonicPage()
@Component({
  selector: 'page-bank-card-add',
  templateUrl: 'bank-card-add.html',
})
export class BankCardAddPage extends SubPage{

  private form: FormGroup
  private canSub: boolean = true

  constructor(public navCtrl: NavController,
              public navParams: NavParams,
              private cc: ControlsCheck,
              private fb: FormBuilder,
              config: Config,
              private userProvider:UserProvider) {
    super(config)
    this.createForm()
  }

  createForm() {
    this.form = this.fb.group({
      account: ['', Validators.compose([Validators.required])],
      // obligatePhone: ['', Validators.compose([Validators.required, Validators.pattern(CONST.REG_PHONE)])]
    })
  }

  submit() {
    if (this.form.valid) {
      if (this.canSub) {
        this.canSub = false
        this.userProvider.addBankCard({
          data: this.form.value,
          complete: ()=>{
            this.canSub = true
          },
          success: ()=>{
            this.navCtrl.pop()
          }
        })
      }
      else return false
    } else {
      this.cc.check(this.form.controls, {
        account: '请输入正确的银行卡号',
        // obligatePhone: '请输入正确的手机号',
      })
    }
  }

  viewDidLoad() {
    console.log('ionViewDidLoad BankCardAddPage');
  }

}
